package com.quanquan.user.controller;

import base.BaseController;
import com.quanquan.user.dto.MarketingRulesDTO;
import com.quanquan.user.exception.UserLevelException;
import com.quanquan.user.request.IntegralRuleRequestBody;
import com.quanquan.user.request.MarketingRulesRequest;
import com.quanquan.user.service.MarketingRulesService;
import enums.ResCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import web.response.ResultBody;

import java.util.List;

/**
 * 功能描述:新用户福利规则设置
 *
 * @ClassName: MarketingRulesController
 * @Author: xiaoqianh丶
 * @Date: 2019-05-24 17:15
 * @Version: V1.0
 */
@RestController
@RequestMapping("/marking_rule")
public class MarketingRulesController extends BaseController {

    private final MarketingRulesService marketingRulesService;

    @Autowired
    public MarketingRulesController(MarketingRulesService marketingRulesService) {
        this.marketingRulesService = marketingRulesService;
    }

    /**
     * 会员等级列表
     *
     * @return
     */
    @PostMapping("/list")
    @ResponseBody
    public ResultBody levelList() {
        List<MarketingRulesDTO> marketingRulesDTOS = marketingRulesService.list();
        if (marketingRulesDTOS.size()<=0) {
            return new ResultBody(ResCodeEnum.FAIL.getCode(), "FAIL");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", marketingRulesDTOS);
    }

    /**
     * 积分规则详情
     *
     * @return
     */
    @PostMapping("/find_by_id")
    @ResponseBody
    public ResultBody findByUserLevelId(@RequestBody IntegralRuleRequestBody requestBody) {
        MarketingRulesDTO marketingRulesDTO = marketingRulesService.findByRuleId(requestBody.getId());
        if (marketingRulesDTO == null) {
            return new ResultBody(ResCodeEnum.FAIL.getCode(), "FAIL");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", marketingRulesDTO);
    }

    /**
     * 更新信息
     *
     * @param requestBody
     * @return
     */
    @PostMapping("/update")
    @ResponseBody
    public ResultBody updateLevel (@RequestBody MarketingRulesRequest requestBody){
        String msg = null;
        try {
            msg = marketingRulesService.updateRule(requestBody);
        } catch (UserLevelException e) {
            e.printStackTrace();
            return new ResultBody(ResCodeEnum.FAIL.getCode(),ResCodeEnum.FAIL.getMsg());
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(),ResCodeEnum.SUCCESS.getMsg(),msg);
    }


    /**
     * 开启满额开关
     * @param
     * @return
     */
    @PostMapping("/turn_on")
    public ResultBody turnOnConsumptionQuota (){
        String msg = marketingRulesService.changeConsumptionQuota();
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(),ResCodeEnum.SUCCESS.getMsg(),msg);
    }


}
